#include "physicsworld.h"
#include "physicsrenderer.h"

namespace gecko
{

	PhysicsWorld::PhysicsWorld( const Vector3f & gravity )
	{
		m_world = new b2World(b2Vec2(gravity.x, gravity.y));
		m_world->SetDebugDraw(&g_physicsRenderer);
		g_physicsRenderer.SetFlags(b2Draw::e_shapeBit | b2Draw::e_jointBit | /*b2Draw::e_aabbBit |*/ b2Draw::e_pairBit | b2Draw::e_centerOfMassBit);
	}

	PhysicsWorld::~PhysicsWorld()
	{
		delete m_world;
	}

	PhysicsObject * PhysicsWorld::CreatePhysicsObject(const BodyDef & def, const Vector3f & position)
	{
		// create physics object
		PhysicsObject * physicsObject = new PhysicsObject(this, def, position);

		// add onto list
		m_physicsObjects.PushBack(physicsObject);

		return physicsObject;
	}

	b2World * PhysicsWorld::GetBox2DWorld()
	{
		return m_world;
	}

	void PhysicsWorld::Update( float dt )
	{
		m_world->Step(dt, 8, 1);
		m_world->ClearForces();
	}

	void PhysicsWorld::RenderDebug()
	{
		m_world->DrawDebugData();
	}

}
